﻿// -----------------------------------------------------------------------
// <copyright file="NodeWFProfileTypeProgressData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------
namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;

    /// <summary>
    /// Class access to db NodeWfDefault
    /// </summary>
    public class NodeWFProfileTypeProgressData
    {
        /// <summary>
        /// Insert to db NodeWfDefault
        /// </summary>
        /// <param name="nodeWFProfileTypeProgress">Paarm nodeWFProfileTypeProgress</param>
        /// <returns>Insert NodeWfDefault</returns>
        public int Insert(NodeWFProfileTypeProgressEN nodeWFProfileTypeProgress)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.NodeWFProfileTypeProgress.NodeWorkflowId, nodeWFProfileTypeProgress.NodeWorkflowId),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWFProfileTypeProgress.ProfileTypeProgressId, nodeWFProfileTypeProgress.ProfileTypeProgressId != 0 ? nodeWFProfileTypeProgress.ProfileTypeProgressId : (object)DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWFProfileTypeProgress.ProfileTypeId, nodeWFProfileTypeProgress.ProfileTypeId != 0 ? nodeWFProfileTypeProgress.ProfileTypeId : (object)DBNull.Value)
                                 };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWfProfileTypeProgressInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete NodeWfDefault by nodeWfDefaultId
        /// </summary>
        /// <param name="nodeWfProfileTypeProgressId">param nodeWfProfileTypeProgressId</param>
        public void Delete(int nodeWfProfileTypeProgressId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.NodeWFProfileTypeProgress.NodeWFProfileTypeProgressId, nodeWfProfileTypeProgressId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWfProfileTypeProgressDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

         /// <summary>
        /// Get List NodeWfDefault By NodeWorkflowId
        /// </summary>
        /// <param name="nodeWorkflowId">Param nodeWorkflowId</param>
        /// <param name="profileTypeId">Param profileTypeId</param>
        /// <returns>Return NodeWfDefault</returns>
        public NodeWFProfileTypeProgressEN GetNodeWfDefaultBy_NodeWorkflowProfileTypeId(int nodeWorkflowId, int profileTypeId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                NodeWFProfileTypeProgressEN nodeWFProfileTypeProgress = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWFProfileTypeProgress_Get_By_NodeWorkflowProfileTypeId, nodeWorkflowId, profileTypeId);
                while (reader.Read())
                {
                    nodeWFProfileTypeProgress = new NodeWFProfileTypeProgressEN();
                    if (!reader.IsDBNull(0))
                    {
                        nodeWFProfileTypeProgress.NodeWFProfileTypeProgressId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        nodeWFProfileTypeProgress.NodeWorkflowId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        nodeWFProfileTypeProgress.ProfileTypeId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        nodeWFProfileTypeProgress.ProfileTypeProgressId = reader.GetInt32(3);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return nodeWFProfileTypeProgress;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all NodeWorkflow
        /// </summary>
        /// <param name="nodeWorkflowId">The node workflow id.</param>
        /// <returns>
        /// Return list all NodeWorkflow
        /// </returns>
        public IList<NodeWFProfileTypeProgressEN> GetNodeWfDefaultBy_NodeWorkflowId(int nodeWorkflowId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<NodeWFProfileTypeProgressEN> lstNodeWFProfileTypeProgress = new List<NodeWFProfileTypeProgressEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWFProfileTypeProgress_Get_By_NodeWorkflowProfileTypeId, nodeWorkflowId, -1);
                while (reader.Read())
                {
                    NodeWFProfileTypeProgressEN nodeWFProfileTypeProgress = new NodeWFProfileTypeProgressEN();

                    if (!reader.IsDBNull(0))
                    {
                        nodeWFProfileTypeProgress.NodeWFProfileTypeProgressId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        nodeWFProfileTypeProgress.NodeWorkflowId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        nodeWFProfileTypeProgress.ProfileTypeId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        nodeWFProfileTypeProgress.ProfileTypeProgressId = reader.GetInt32(3);
                    }

                    lstNodeWFProfileTypeProgress.Add(nodeWFProfileTypeProgress);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstNodeWFProfileTypeProgress;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Check Exit
        /// </summary>
        /// <param name="nodeWorkflowId">param nodeWorkflowId</param>
        /// <param name="profileTypeProgressId">param profileTypeProgressId</param>
        /// <param name="profileTypeId">param profileTypeId</param>
        /// <returns>Is valid</returns>
        public bool Exist(int nodeWorkflowId, int profileTypeProgressId, int profileTypeId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.NodeWFProfileTypeProgress.NodeWorkflowId, nodeWorkflowId),
                    new SqlParameter(Constants.SqlParamerterName.NodeWFProfileTypeProgress.ProfileTypeProgressId, profileTypeProgressId),
                    new SqlParameter(Constants.SqlParamerterName.NodeWFProfileTypeProgress.ProfileTypeId, profileTypeId)
                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWFProfileTypeProgressExist, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}